package com.lonefennec.chameleon.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.lonefennec.chameleon.ChameleonGame;

public class AlertLevel extends Actor
{
	private Sprite alertIcon;
	//private Sprite alertBar;
	private Texture alertBarTexture;
	private Rectangle alertBar;
	
	private BitmapFont font;
	
	private float fullBarWidth;
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha)
	{		
		int barWidth = (int)(fullBarWidth * ChameleonGame.state.alertLevel);
		
		if (barWidth > 0)
		{	
			float rightU = (float) barWidth / fullBarWidth;
			
			batch.draw(alertBarTexture, alertBar.x, alertBar.y, barWidth, alertBar.height, 0, 0, rightU, 1);
			
			String message = "Hide!!";
			TextBounds messageBounds = font.getBounds(message);
			
			float messageX = alertBar.getX() + (fullBarWidth/2 - messageBounds.width / 2);
			float messageY = alertBar.getY() - 12;
			
			font.draw(batch, message, messageX, messageY);
		}
	}
	
	@Override
	public void act(float delta)
	{
		
	}
	
	@Override
	public Actor hit(float x, float y)
	{
		return null;
	}
	
	public AlertLevel()
	{
		String iconPath = "ui/alert.png";
		String barPath = "ui/alert-bar.png";
		String fontPath = "fonts/serif-large.fnt";
		
		ChameleonGame.assets.load(iconPath, Texture.class);
		ChameleonGame.assets.load(barPath, Texture.class);
		ChameleonGame.assets.load(fontPath, BitmapFont.class);
		ChameleonGame.assets.finishLoading();
		
		alertIcon = new Sprite(
				ChameleonGame.assets.get(iconPath, Texture.class));
		alertBarTexture = ChameleonGame.assets.get(barPath, Texture.class);
		
		font = ChameleonGame.assets.get(fontPath, BitmapFont.class);
		
		alertIcon.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		alertBarTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		int screenWidth = Gdx.graphics.getWidth();
		int screenHeight = Gdx.graphics.getHeight();
		
		float alertIconHeight = screenHeight * 0.15f;
		float alertIconWidth = alertIconHeight;
				
		float iconLeft = screenWidth / 2 - alertIconWidth / 2;
		float iconTop = screenHeight - alertIconHeight * 1.2f;
		
		alertIcon.setBounds(iconLeft ,iconTop, alertIconWidth, alertIconHeight);
		
		fullBarWidth = screenWidth * 0.6f;
		float barHeight = screenHeight * 0.15f;
		float barTop = screenHeight - barHeight*1.2f;
		float barLeft = screenWidth/2 - fullBarWidth /2;
		
		alertBar = new Rectangle(barLeft, barTop, fullBarWidth, barHeight);
	}
}
